package edu.hawaii.halealohacli.jcev;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.wattdepot.client.WattDepotClient;
import edu.hawaii.halealohacli.jcev.processor.CommandProcessor;
import edu.hawaii.halealohacli.jcev.processor.ExitException;

/**
 * Main class of the hale-aloha-cli-jcev project which runs the Command Line Interface.
 * @author Christopher Foo
 *
 */
public class Control {
  /**
   * Creates a client to the WattDepot server at the given url and checks if it is healthy.
   * @param url The url to the WattDepot server to connect to.
   * @return The WattDepotClient to the specified WattDepotServer or null if the client is not
   *         healthy.
   */
  public WattDepotClient getClient(String url) {
    // Create client and check that it is healthy.
    WattDepotClient client = new WattDepotClient(url);

    if (client.isHealthy()) {
      System.out.println("Successfully connected to the WattDepot server");
      return client;
    }
    else {
      System.out.println("Error: Unable to connect to the WattDepot server at " + url);
      return null;
    }
  }

  /**
   * Gets the name of the command from the line that the user input.
   * @param userInput The line of user input.
   * @return The name of the command without whitespace and in lowercase.
   */
  public String getCommandName(String userInput) {
    int endOfCommand = userInput.indexOf(' ');
    if (endOfCommand == -1) {
      return userInput.trim().toLowerCase();
    }
    else {
      return userInput.substring(0, endOfCommand).trim().toLowerCase();
    }
  }

  /**
   * Gets the arguments for the command from the line of user input.
   * @param userInput The line of user input.
   * @return The arguments passed to the command or an empty string if no arguments were passed.
   */
  public String getArgs(String userInput) {
    int endOfCommand = userInput.indexOf(' ');
    if (endOfCommand == -1) {
      return "";
    }
    else {
      return userInput.substring(endOfCommand).trim();
    }
  }

  /**
   * Runs the Command Line Interface for the hale-aloha-cli-jcev project.
   * @param args None expected or used.
   */
  public static void main(String[] args) {
    Control controller = new Control();
    String url = "http://server.wattdepot.org:8190/wattdepot/";
    WattDepotClient client = controller.getClient(url);
    
    //If client is not healthy then exit
    if (client == null) {
      return;
    }
    
    InputStreamReader inputReader = new InputStreamReader(System.in);
    BufferedReader buffReader = new BufferedReader(inputReader);
    CommandProcessor processor = new CommandProcessor();
    
    //Infinite loop.
    while (true) {
      System.out.print(">");
      try {
        String input = buffReader.readLine();
        String commandName = controller.getCommandName(input);
        String arguments = controller.getArgs(input);
        try {
          processor.execute(client, commandName, arguments);
        }
        catch (ExitException e) {
          return;
        }
        catch (Exception e) {
          System.out.println(e.getMessage());
        }
      }
      catch (IOException e) {
        System.out.println("Error: Unable to read input line");
      }
    }
  }
}
